home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Especial Multimedia
/
Especial Multimedia.iso
/
Multimed
/
Prg
/
STR2BMP.ZIP
/
STR2BMP.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-09-14
|
16KB
|
583 lines
// Strbmp.cpp : Code for VB string to bitmap conversion DLL.
//
// Freeware -- Version 1.1 09/20/93
//
// Written by Jorge Monasterio (CIS 72147, 2674)
//
//
/* Header files. */
#include <windows.h>
#include <string.h>
#include "c:\vb\cdk\vbapi.h"
#include "str2bmp.h"
#pragma hdrstop
// DESCRIPTION: DLL Initialization code.
// PARAMETERS: Windows stuff.
// RETURN VALUE: 1 if succesfully initialization, else 0.
int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg, WORD cbHeapSize, LPSTR lpCmdLine)
{ return 1;
}
// DESCRIPTION: DLL removal code.
// PARAMETERS: Windows stuff.
// RETURN VALUE: 1 if succesfully initialization, else 0.
int CALLBACK WEP (int nParameter)
{ // Dll removal successful.
return 1;
}
// DESCRIPTION: Display a runtime error message to VB and return error code.
// RETURN VALUE: **** THIS FUNCTION DOES NOT RETURN ****
DLL(void) ReturnVBError( ERR err, LPSTR msg)
{ VBRuntimeError( VBSetErrorMessage( err, msg));
}
// DESCRIPTION:
// Function for converting VB bitmap to string.
// PARAMETERS:
// hdc : Picture.hdc
// himage : Picture.image
// string : VB string. Must be large enough to hold data. See
// Bmp_GetSize for more info.
// RETURN VALUE:
// VB string containing the bitmap information.
// SYNTAX IN VB:
// A$ = BmpToString( Picture1.hdc, Picture1.image)
DLL(HLSTR) BMPToString( HDC hdc, HBITMAP hImage)
{ // Local variables.
HBITMAP hOldBitmap;
BITMAP bm;
HLSTR hlstr;
LPSTR RESULT;
// LPSTR string replaced with VBDeref(hlstr)
if( BMP_GetSize( hImage) > 0)
{ hlstr = VBCreateTempHlstr( NULL, BMP_GetSize(hImage));
RESULT = VBDerefHlstr( hlstr);
}
else
{ // Error occurred -- image too big to fit in the string.
ReturnVBError( OUT_OF_STRING_SPACE, "Bitmap too big to fit in string.");
}
// Get information about bitmap size and store in bm.
GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
// Select VB's picture... GET BITMAP DATA.
hOldBitmap = (HBITMAP) SelectObject( hdc, hImage);
// Store the width and height in a header at beginning of string
StringBMP_SetDimensions( RESULT, bm.bmWidth, bm.bmHeight, bm.bmWidthBytes);
// Copy bitmap data into string.
GetBitmapBits( hImage, bm.bmWidthBytes * bm.bmHeight, RESULT+sizeof( SB));
// DeSelect VB's picture.
SelectObject( hdc, hOldBitmap);
// Return the temporary VB string.
return hlstr;
}
// DESCRIPTION:
// Function for converting string to a bitmap.
// PARAMETERS:
// string : VB string. Must contain valid bitmap info created using
// BmpToString.
// hdcSrc : VB Source Picture.hdc
// hbmpSrc : VB Source Picture.image
// RETURN VALUE:
// ON SUCCESS: Number of pixels in the image.
// ON FAILURE: Return VB runtime error.
// SYNTAX IN VB:
// ret& = StringToBmp( A$, Picture1.hdc, Picture1.image)
DLL(LONG) StringToBMP( LPSTR A, HDC hdcSrc, HBITMAP hbmpSrc)
{ // Local variables.
HBITMAP hOldBitmapNew;
HBITMAP hOldBitmapSrc;
HBITMAP hNewBitmap;
HDC hdcNew;
WORD wA, hA, wbA;
// Make sure parameter is bitmap string.
StringBMP_ValidateParameters( A,NULL);
// Get width and height from first chars of string.
wA = StringBMP_GetWidth(A);
hA = StringBMP_GetHeight(A);
wbA = StringBMP_GetWidthBytes(A);
// Create a new bitmap.
hdcNew = CreateCompatibleDC( hdcSrc);
hOldBitmapSrc = (HBITMAP) SelectObject( hdcSrc, hbmpSrc);
hNewBitmap = CreateCompatibleBitmap( hdcSrc, wA, hA);
// Select the new bitmap.
hOldBitmapNew = (HBITMAP) SelectObject( hdcNew, hNewBitmap);
// Copy bitmap data into string.
SetBitmapBits( hNewBitmap, wbA*hA, A+sizeof( SB));
// Copy the new bitmap to VB's
BitBlt( hdcSrc, 0,0,wA,hA, hdcNew, 0,0, SRCCOPY);
// DeSelect bitmaps.
SelectObject( hdcSrc, hOldBitmapSrc);
SelectObject( hdcNew, hOldBitmapNew);
// Delete the new bitmap.
DeleteObject( hNewBitmap);
DeleteDC( hdcNew);
return (wbA*hA);
}
// DESCRIPTION:
// Return required size in bytes for a string to hold a bitmap.
// PARAMETERS:
// hImage : VB Picture.image
// RETURN VALUE:
// ON SUCCESS: Number of bytes to initialize a string size.
// ON FAILURE: Runtime error returned to VB.
// SYNTAX IN VB:
// bytes& = Bmp_GetSize( Picture1.Image)
// if bytes = 0 then stop ' Error! Picture too big to fit in string.
//
DLL(LONG) BMP_GetSize( HBITMAP hImage)
{ // Local variables.
DWORD bwB, hB;
// Get information about bitmap size.
BITMAP bm;
GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
bwB = bm.bmWidthBytes;
hB = bm.bmHeight;
// Check for error condition
if( (bwB * hB + sizeof( SB) )>MAX_VB_STRING_LENGTH)
{ return 0;
}
// Calculate size of string
return( sizeof( SB) + hB * bwB);
}
// DESCRIPTION:
// Binary COMPARE two bitmap strings.
// PARAMETERS:
// A, B: String to be compared.
// RETURN VALUE:
// On success: Return the number of pixels difference.
// On error: VB runtime error.
// SYNTAX IN VB:
// bytes& = StringBMP_COMPARE( A$, B$)
//
DLL(LONG) StringBMP_Compare( LPSTR A, LPSTR B)
{ // Local variables.
WORD wA, hA, wbA;
WORD h,w;
DWORD DifferenceCount;
LPSTR ptrA, ptrB, ptrLINEA, ptrLINEB;
// Validate parameters.
StringBMP_ValidateParameters( A,B);
// Get bitmap dimensions.
wA = StringBMP_GetWidth( A);
hA = StringBMP_GetHeight( A);
wbA = StringBMP_GetWidthBytes( A);
// Set Pointers to beginning of data in string.
ptrLINEA = A + sizeof( SB);
ptrLINEB = B + sizeof( SB);
// Compare the strings.
DifferenceCount = 0L;
for( h=0; h<hA; h++)
{ ptrA = ptrLINEA;
ptrB = ptrLINEB;
ptrLINEA += wbA;
ptrLINEB += wbA;
for( w=0; w<wA; w++)
{ if (*(ptrA++) != *(ptrB++))
{ DifferenceCount++; // Count # of deltas.
}
}
}
return DifferenceCount;
}
// DESCRIPTION:
// Binary OR two bitmap strings.
// PARAMETERS:
// A, B: VB strings to be or'ed.
// RETURN VALUE:
// On success: Return the or'ed string.
// On error: VB runtime error.
// SYNTAX IN VB:
// C$ = StringBMP_OR( A$, B$)
//
DLL(HLSTR) StringBMP_OR( LPSTR A, LPSTR B)
{ // Local variables.
WORD wbA, hA, wA;
WORD h, w;
LPSTR ptrA, ptrB, ptrRESULT;
LPSTR ptrLINEA, ptrLINEB, ptrLINERESULT;
LPSTR RESULT;
HLSTR hlstr;
// Validate parameters.
StringBMP_ValidateParameters( A,B);
// Get bitmap dimensions.
wA = StringBMP_GetWidth( A);
hA = StringBMP_GetHeight( A);
wbA = StringBMP_GetWidthBytes( A);
// Create temporary VB string. RESULT = pointer to string.
hlstr = VBCreateTempHlstr( NULL, sizeof( SB) + hA * wbA);
RESULT = VBDerefHlstr( hlstr);
// Set dimensions of output string.
StringBMP_SetDimensions( RESULT, wA, hA, wbA);
// Set Pointers to beginning of data in string.
ptrLINEA = A + sizeof( SB);
ptrLINEB = B + sizeof( SB);
ptrLINERESULT = RESULT + sizeof( SB);
// OR the strings.
for( h=0; h<hA; h++)
{ ptrA = ptrLINEA;
ptrB = ptrLINEB;
ptrRESULT = ptrLINERESULT;
ptrLINEA += wbA;
ptrLINEB += wbA;
ptrLINERESULT += wbA;
for( w=0; w<wA; w++)
{ *(ptrRESULT++) = (*(ptrA++) & *(ptrB++));
}
}
return hlstr;
}
// DESCRIPTION:
// Binary AND two bitmap strings.
// PARAMETERS:
// A, B: VB strings to be and'ed.
// RETURN VALUE:
// On success: Return the and'ed string.
// On error: VB runtime error.
// SYNTAX IN VB:
// C$ = StringBMP_AND( A$, B$)
//
DLL(HLSTR) StringBMP_AND( LPSTR A, LPSTR B)
{ // Local variables.
WORD wbA, hA, wA;
WORD h, w;
LPSTR ptrA, ptrB, ptrRESULT;
LPSTR ptrLINEA, ptrLINEB, ptrLINERESULT;
LPSTR RESULT;
HLSTR hlstr;
// Validate parameters.
StringBMP_ValidateParameters( A,B);
// Get bitmap dimensions.
wA = StringBMP_GetWidth( A);
hA = StringBMP_GetHeight( A);
wbA = StringBMP_GetWidthBytes( A);
// Create temporary VB string. RESULT = pointer to string.
hlstr = VBCreateTempHlstr( NULL, hA*wbA+sizeof(SB));
RESULT = VBDerefHlstr( hlstr);
// Set dimensions of output string.
StringBMP_SetDimensions( RESULT, wA, hA, wbA);
// Set Pointers to beginning of data in string.
ptrLINEA = A + sizeof( SB);
ptrLINEB = B + sizeof( SB);
ptrLINERESULT = RESULT + sizeof( SB);
// AND the strings.
for( h=0; h<hA; h++)
{ ptrA = ptrLINEA;
ptrLINEA += wbA;
ptrB = ptrLINEB;
ptrLINEB += wbA;
ptrRESULT = ptrLINERESULT;
ptrLINERESULT += wbA;
for( w=0; w<wA; w++)
{ *(ptrRESULT++) = (*(ptrA++) | *(ptrB++));
}
}
// Return VB string.
return hlstr;
}
// DESCRIPTION:
// Binary INVERT a bitmap string.
// PARAMETERS:
// A: VB string to be inverted.
// RETURN VALUE:
// On success: Return the inverted string.
// On error: VB runtime error.
// SYNTAX IN VB:
// A$ = StringBMP_INVERT( B$)
//
DLL(HLSTR) StringBMP_INVERT( LPSTR A)
{ // Local variables.
LONG i, size;
LPSTR ptrA, ptrRESULT;
LPSTR RESULT;
HLSTR hlstr;
// Validate parameters.
StringBMP_ValidateParameters( A,NULL);
// Calculate size in bytes.
size = StringBMP_GetWidthBytes(A)*StringBMP_GetHeight(A);
// Create temporary VB string. RESULT = pointer to string.
hlstr = VBCreateTempHlstr( NULL, size+sizeof(SB));
RESULT = VBDerefHlstr( hlstr);
// Set Pointers to beginning of data in string.
ptrA = A + sizeof( SB);
ptrRESULT = RESULT + sizeof( SB);
// Invert the string.
for( i=0; i<size; i++)
{ *(ptrRESULT++) = ~*(ptrA++);
}
return (hlstr);
}
// DESCRIPTION:
// Fills a bitmap with same color pixels.
// PARAMETERS:
// A: VB string to be filled with bitmap.
// CHAR: VB string containing fill character. Only leftmost
// character is used.
// RETURN VALUE:
// On success: Return the filled string.
// On error: VB runtime error.
// SYNTAX IN VB:
// A$ = StringBMP_Fill( B$, chr$(0))
//
DLL(HLSTR) StringBMP_Fill( LPSTR A, LPSTR CHAR)
{ // Local variables.
LONG i, size;
LPSTR ptrRESULT;
LPSTR RESULT;
HLSTR hlstr;
// Validate parameters.
StringBMP_ValidateParameters( A, NULL);
// Calculate size in bytes.
size = StringBMP_GetWidthBytes(A)*StringBMP_GetHeight(A);
// Create temporary VB string. RESULT = pointer to string.
hlstr = VBCreateTempHlstr( NULL, size+sizeof(SB));
RESULT = VBDerefHlstr( hlstr);
// Set Pointers to beginning of data in string.
ptrRESULT = RESULT + sizeof( SB);
// Fill the string with the leftmost character in A.
for( i=0; i<size; i++)
{ *(ptrRESULT++)=CHAR[0];
}
// Return the filled string.
return hlstr;
}
// DESCRIPTION:
// Returns the height in pixels of a bitmap.
// PARAMETERS:
// hImage : VB Picture.image
// RETURN VALUE:
// Height in pixels.
// SYNTAX IN VB:
// Should not be called from VB.
//
DLL(LONG) BMP_GetHeight( HBITMAP hImage)
{ // Get information about bitmap size.
BITMAP bm;
GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
return ( bm.bmHeight);
}
// DESCRIPTION:
// Returns the width in pixels of a bitmap.
// PARAMETERS:
// hImage : VB Picture.image
// RETURN VALUE:
// Width in pixels.
// SYNTAX IN VB:
// Should not be called from VB.
//
DLL(LONG) BMP_GetWidth( HBITMAP hImage)
{ // Get information about bitmap size.
BITMAP bm;
GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
return( bm.bmWidthBytes);
}
// DESCRIPTION:
// Store the dimensions of the bitmap in a header at the beginning
// of the string.
// PARAMETERS:
// A : VB string
// w : width of VB bitmap to be stored in the string.
// h : height of VB bitmap to be stored in the string.
// wb : width in bytes (always even) of VB bitmap to be stored.
// RETURN VALUE:
// ON SUCCESS: 1
// SYNTAX IN VB:
// Should not be called from VB.
//
DLL(LONG) StringBMP_SetDimensions( LPSTR A, int w, int h, int wb)
{ union STRINGBMP temp;
temp.vbstring = A;
temp.stringbmp->identifier = StringBmpIdentifier;
temp.stringbmp->width = w;
temp.stringbmp->height = h;
temp.stringbmp->widthbytes = wb;
return 1;
}
// DESCRIPTION:
// Examine the header of a string containing bitmap info and
// determine the bitmap width.
// PARAMETERS:
// A : VB string containing bitmap info.
// RETURN VALUE:
// width in pixels of the stored bitmap.
// SYNTAX IN VB:
// Should not be called from VB.
//
DLL(WORD) StringBMP_GetWidth( LPSTR A)
{ union STRINGBMP temp;
//StringBMP_ValidateParameters( A,NULL);
temp.vbstring = A;
return temp.stringbmp->width;
}
// DESCRIPTION:
// Examine the header of a string containing bitmap info and
// determine the bitmap height.
// PARAMETERS:
// A : VB string containing bitmap info.
// RETURN VALUE:
// Height in pixels of the stored bitmap.
// SYNTAX IN VB:
// Should not be called from VB.
//
DLL(WORD) StringBMP_GetHeight( LPSTR A)
{ union STRINGBMP temp;
//StringBMP_ValidateParameters( A,NULL);
temp.vbstring = A;
return temp.stringbmp->height;
}
// DESCRIPTION:
// Examine the header of a string containing bitmap info and
// determine the bitmap width in bytes. (Always even).
// PARAMETERS:
// A : VB string containing bitmap info.
// RETURN VALUE:
// Width in bytes of the stored bitmap.
// SYNTAX IN VB:
// Should not be called from VB.
//
DLL(WORD) StringBMP_GetWidthBytes( LPSTR A)
{ union STRINGBMP temp;
//StringBMP_ValidateParameters( A,NULL);
temp.vbstring = A;
return temp.stringbmp->widthbytes;
}
// DESCRIPTION:
// Examine the header of a string containing bitmap info and
// return the "identifier word". This word is used to check
// whether the string contains valid bitmap info.
// PARAMETERS:
// A : VB string containing bitmap info.
// RETURN VALUE:
// Identifier word. Should be 'SB' (0x5342)
// SYNTAX IN VB:
// Should not be called from VB.
DLL( WORD) StringBmp_VerifyIdentifier( LPSTR A)
{ union STRINGBMP temp;
temp.vbstring = A;
if( temp.stringbmp->identifier != StringBmpIdentifier)
{ ReturnVBError(ILLEGAL_FUNCTION_CALL,"String does not contain a valid bitmap.");
}
return temp.stringbmp->identifier;
}
// DESCRIPTION:
// Examine the header of two strings containing bitmap info and
// make sure they are the same size.
// PARAMETERS:
// A : VB string containing bitmap info.
// B : VB string containing bitmap info.
// RETURN VALUE:
// ON SUCCESS: 0 = bitmaps are the same size.
// ON FAILURE: Return VB runtime error.
// SYNTAX IN VB:
// Should not be called from VB.
DLL(WORD) StringBMP_ValidateParameters( LPSTR A, LPSTR B)
{ WORD wA, hA, wbA;
WORD wB, hB, wbB;
// Check bitmap A:
if (A != NULL)
{ StringBmp_VerifyIdentifier( A);
wA = StringBMP_GetWidth( A);
hA = StringBMP_GetHeight( A);
wbA = StringBMP_GetWidthBytes( A);
}
// Get size of bitmap B.
if( B != NULL)
{ StringBmp_VerifyIdentifier( B);
wB = StringBMP_GetWidth( B);
hB = StringBMP_GetHeight( B);
wbB = StringBMP_GetWidthBytes( B);
}
// Make sure all bitmaps the same size.
if( (A != NULL) && (B !=NULL) )
{ if ( !( (wbA == wbB) && (hA == hB) && (wA == wB) ) )
{ ReturnVBError(ILLEGAL_FUNCTION_CALL,"Function requires two same-size strings.");
}
}
// Everything OK.
return 0;
}